/***************************************************************************
 *   Copyright (C) 2006 by Archimedes Projekt   *
 *   thelsb@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef HPSCCOMMADTHREAD_H
#define HPSCCOMMADTHREAD_H

#include <QThread>
#include <QList>
#include <QPair>
#include <QMutex>
#include <QEvent>
#include "gssglobals.h"
//#include <QString>
//#include "qextserialport.h"


class QString;
class QextSerialPort;




class HPSCEventBase : public QEvent
{
	private:
		HPSC_Command m_command;
		QString m_strCommand;
		QPair<QString,long> m_strCmdWait;
        bool m_serialConnect;
	public:
		HPSCEventBase(Type type /*= HPSCMEAS*/,HPSC_Command command):QEvent(type),
		m_command(command){}
		HPSCEventBase(Type type /*= HPSCMEAS*/,QString command):QEvent(type),
		m_strCommand(command){}
		HPSCEventBase(Type type /*= HPSCMEAS*/,QPair<QString,long> command):QEvent(type),
		m_strCmdWait(command){}
        HPSCEventBase(Type type /*= HPSCMEAS*/,bool command):QEvent(type),
        m_serialConnect(command){}
		inline HPSC_Command command(){ return m_command; }
		inline QString strCommand(){ return m_strCommand; }
		inline QPair<QString,long> strCmdWait(){ return m_strCmdWait; }
        inline bool serialConnect(){ return m_serialConnect; }
		~HPSCEventBase(){}
};





/**
A thread class for the HPSC commands.
Each command is executed in its own thread to allow the wait times bween commands with out disturbing the main GUI thread

	@author Archimedes Projekt <thelsb@gmail.com>
 */

class HPSCCommandThread : public QThread
{
	Q_OBJECT
	private:
		QextSerialPort *m_serial;
		QList<QPair<QString,long> > m_listCommands;
		QMutex m_serialMutex,
			   m_commandListMutex;		
		
public:
	HPSCCommandThread(QextSerialPort *serial);

    ~HPSCCommandThread();
    void run();
    void postCommand(QString cmd,long usWait=30000);
    //void executeCommand(QString cmd,long usWait);
    //void execute();
    void customEvent(QEvent *event);
	
	signals:
		void sig_readoutV(int,QString);
		void sig_readoutA(int,QString);
		void sig_readout(QString);
        void sig_pollNext();

};

#endif
